import { DialogForm } from 'custom/index'
import { updateAccountInputRecord, accountInputRecord } from './api'
import HalfInput from '../data-query-input/component/HalfInput.vue'
import CheckIgnore from './components/CheckIgnore.vue'

const inputDataDialog = new DialogForm('DialogForm')

inputDataDialog
  .title('录入数据')
  .getApi(async (_ ,row) => ({ ...row }))
  .postApi(updateAccountInputRecord)
  .useGroup('')
    .useDatePicker('date').label('服务日期').setProp('disabled', true).end()
    .useItem('accountNo').label('数据账号').setProp('disabled', true).end()
    .useItem('accountName').label('子账号名称').setProp('disabled', true).end()
    .useCustomItem(HalfInput, 'salesRevenue').label('销售额(元)').required().end()
    .useCustomItem(HalfInput, 'receptionVolume').label('接待量(人)').required().end()
    .useItem('conversionRate').label('转化率(%)').required().end()
    .useItem('responseTime').label('响应时间(秒)').required().end()
    .useItem('qaratio').label('问答比(%)').required().end()
    .useItem('responseRate').label('回复率(%)').required().end()

const settingDialog = new DialogForm('DialogForm')
settingDialog
  .title('批量设置忽略汇总项')
  .getApi(async (_, row) => {
    const ignores = []
    if (row.conversionRateIgnore) {
      ignores.push('conversionRateIgnore')
    }
    if (row.responseTimeIgnore) {
      ignores.push('responseTimeIgnore')
    }
    if (row.qARatioIgnore) {
      ignores.push('qARatioIgnore')
    }
    if (row.responseRateIgnore) {
      ignores.push('responseRateIgnore')
    }
    return { ignores: ignores, inputRecordIds: [row.inputRecordId] }
  })
  .postApi(data => {
    const { inputRecordIds, ignores } = data
    const conversionRateIgnore = ignores.includes('conversionRateIgnore') ? 1 : 0
    const responseTimeIgnore  = ignores.includes('responseTimeIgnore') ? 1 : 0
    const qARatioIgnore = ignores.includes('qARatioIgnore') ? 1 : 0
    const responseRateIgnore = ignores.includes('responseRateIgnore') ? 1 : 0
    const d = {
      inputRecordIds,
      conversionRateIgnore,
      responseTimeIgnore,
      qARatioIgnore,
      responseRateIgnore
    }
    return accountInputRecord(d)
  })
  .useGroup('')
  .useCustomItem(CheckIgnore, 'ignores').label('忽略选项').setDefault([]).end()

const bacthsettingDialog = new DialogForm('DialogForm')
bacthsettingDialog
  .title('批量设置忽略汇总项')
  .getApi(async (_, row) => ({ ignores: [], inputRecordIds: row.selection.map(i => i.inputRecordId) }))
  .useGroup('')
    .useCustomItem(CheckIgnore, 'ignores').label('忽略选项').setDefault([]).end()



  import { ElMessage } from 'element-plus'
  
  export async function onBatch({ selection }) {
    if (!selection.length) {
      ElMessage.warning('请选择数据')
      return Promise.reject()
    }
    const data = await bacthsettingDialog.open({ row: { selection } })
    const { inputRecordIds, ignores } = data as any
    const conversionRateIgnore = ignores.includes('conversionRateIgnore') ? 1 : 0
    const responseTimeIgnore  = ignores.includes('responseTimeIgnore') ? 1 : 0
    const qARatioIgnore = ignores.includes('qARatioIgnore') ? 1 : 0
    const responseRateIgnore = ignores.includes('responseRateIgnore') ? 1 : 0
    const d = {
      inputRecordIds,
      conversionRateIgnore,
      responseTimeIgnore,
      qARatioIgnore,
      responseRateIgnore
    }
    await accountInputRecord(d)
    ElMessage.success('操作成功')
  }
export { inputDataDialog, settingDialog }
